﻿using AutoMapper;
using Max.PH5_2103A.Shopping.TEntityFrame.Domain;
using Max.PH5_2103A.Shopping.TEntityFrame.DTO;
using Max.PH5_2103A.Shopping.TEntityFrame.IRepository;
using Max.PH5_2103A.Shopping.TEntityFrame.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2103A.Shopping.TEntityFrame.Service
{
    public class OrderMasterService : BaseService<OrderMasterEntity>, IOrderMasterService
    {
        IUnitOfWork _unitOfWork;
        IOrderMasterRepository _masterRepository;
        IOrderDetailRepository _detailRepository;
        IMapper _map;
        ISKURepository _skuRepository;
        public OrderMasterService(IOrderMasterRepository repository, IUnitOfWork unitOfWork, IOrderDetailRepository detailRepository, IMapper map, ISKURepository skuRepository) : base(repository, unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _masterRepository = repository;
            _detailRepository = detailRepository;
            _map = map;
            _skuRepository = skuRepository;
        }




        //添加订单
        public ResponseStateCode Insert(OrderInputDto dto)
        {
            using (var tran = _unitOfWork.GetContext().Database.BeginTransaction())
            {
                try
                {
                    #region 添加订单数据（订单表和订单明细表）

                    var master = _map.Map<OrderInputDto, OrderMasterEntity>(dto);
                    _masterRepository.Insert(master);
                    var details = _map.Map<List<OrderDetailInputDto>, List<OrderDetailEntity>>(dto.Details);
                    details.ForEach(x => x.OrderNo = master.OrderNo);
                    _detailRepository.Insert(details);

                    #endregion

                    #region 修改库存（还需要判断库存）

                    details.ForEach(x =>
                    {
                        var m = _skuRepository.Select().Where(p => p.Id == x.SKUId).FirstOrDefault();
                        if(m!= null)
                        {
                            m.LockStock += x.Number;  //锁定库存增加
                            m.EnableStock -= x.Number; //可用库存减少
                            _skuRepository.Update(m);
                        }
                    });

                    #endregion

                    #region 从购物车删除

                    CSRedis.CSRedisClient client = new CSRedis.CSRedisClient("127.0.0.1:6390");
                    string key = dto.MemberId.ToString();
                    client.HDel(key, details.Select(x => x.Id.ToString()).ToArray());

                    #endregion

                    _unitOfWork.SaveChanges();

                    tran.Commit();
                    return  ResponseStateCode.Success;
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                    return  ResponseStateCode.Failed;
                }
            }

        }
    }
}
